package com.example.trabajofinal.activity;

import com.example.trabajofinal.MyApplication;
import com.example.trabajofinal.R;
import com.example.trabajofinal.db.DbHelper;
import com.example.trabajofinal.model.User;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity
{
	static final int DIALOG_NEW_USER = 1;
	private TextView tvUser;
	private TextView tvPass;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity);
		tvUser = (TextView)findViewById(R.id.username);
		tvPass = (TextView)findViewById(R.id.password);
	}

	
	@Override
	protected void onResume() {
		super.onResume();

		tvUser.setText("");
		tvPass.setText("");
	}
	
	public void btnLoginClicked (View v)
	{
		String user = tvUser.getText().toString();
		String pass = tvPass.getText().toString();

		DbHelper dbHelper = DbHelper.getDatabaseInstance(LoginActivity.this);
		User ouser = dbHelper.getUser(user);
		if(ouser != null && ouser.validatePwd(pass)){
			MyApplication.CurrentUser = user;
			Intent i = new Intent(this, CursosActivity.class);
			startActivity(i);
		}
		else{
			//Toast.makeText(this, "Usuario o Clave Incorrecta", Toast.LENGTH_LONG).show();

			new AlertDialog.Builder(LoginActivity.this)
			.setTitle("Error")
			.setMessage("Usuario o Clave Incorrecta")
			.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) { 
				}
			}).show();

			//Limpiar cuadros de texto
			tvUser.setText("");
			tvPass.setText("");
		}
	}

	public void btnCreateClicked(View v){
		showDialog(DIALOG_NEW_USER);
	}


	@Override
	protected Dialog onCreateDialog(int id) {

		Dialog dialog = null;

		switch(id) {
		case DIALOG_NEW_USER:
			AlertDialog.Builder b = new AlertDialog.Builder(this);
			b.setTitle("Crear Nueva Cuenta");
			LayoutInflater inflater = getLayoutInflater();
			b.setView(inflater.inflate(R.layout.new_account_dialog, null));			

			b.setPositiveButton("Crear", new android.content.DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					EditText etName = (EditText)((AlertDialog)(dialog)).findViewById(R.id.user_name);
					EditText etUser = (EditText)((AlertDialog)(dialog)).findViewById(R.id.user_user);
					EditText etPwd1 = (EditText)((AlertDialog)(dialog)).findViewById(R.id.user_password1);
					EditText etPwd2 = (EditText)((AlertDialog)(dialog)).findViewById(R.id.user_password2);
					boolean correcto = true;

					if(!etPwd1.getText().toString().equals(etPwd2.getText().toString())){
						new AlertDialog.Builder(LoginActivity.this)
						.setTitle("Error")
						.setMessage("El password ingresado no es correcto")
						.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) { 
							}
						}).show();

						correcto = false;
					}

					DbHelper dbHelper = DbHelper.getDatabaseInstance(LoginActivity.this);

					if(dbHelper.getUser(etUser.getText().toString()) != null && correcto){
						new AlertDialog.Builder(LoginActivity.this)
						.setTitle("Error")
						.setMessage("El nombre de usuario ya existe")
						.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) { 
							}
						}).show();

						tvUser.setText("");
						tvPass.setText("");
						
						correcto = false;
					}

					if(correcto){
						dbHelper.insertUser(etUser.getText().toString(), etName.getText().toString(), etPwd1.getText().toString());
						Log.d("Final", "Crear usuario: " + etName.getText() + ", " + etUser.getText() + ", " + etPwd1.getText());
						
						Toast.makeText(LoginActivity.this, "Nuevo usuario creado", Toast.LENGTH_LONG).show();
					
						MyApplication.CurrentUser = etUser.getText().toString();						
						Intent i = new Intent(LoginActivity.this, CursosActivity.class);
						startActivity(i);
					}
				}
			});

			b.setNegativeButton("Cancelar", new android.content.DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});

			dialog = b.create();
			return dialog;
		default:
			return null;
		}	
	}
}
